package com.xoa.service.financialManage.charge.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xoa.dao.financialManage.charge.ChargePermissionMapper;
import com.xoa.model.financialManage.charge.ChargePermission;
import com.xoa.model.financialManage.charge.ChargePermissionExample;
import com.xoa.model.financialManage.charge.ChargePermissionWithBLOBs;
import com.xoa.service.financialManage.charge.ChargePermissionService;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限设置 服务实现类
 * </p>
 *
 * @author liujian
 * @since 2020-04-23
 */
@Service
public class ChargePermissionServiceImpl extends ServiceImpl<ChargePermissionMapper, ChargePermission> implements ChargePermissionService {

    @Autowired
    private ChargePermissionMapper chargePermissionMapper;

    @Override
    public ToJson findPermissionAll(ChargePermissionWithBLOBs chargePermission, Integer page, Integer limit) {
        ToJson toJson=new ToJson(1,"暂无数据");
        Map map=new HashMap();
        try {
            PageParams pageParams=new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(limit);
            pageParams.setUseFlag(true);
            map.put("pageParams",pageParams);
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            List<ChargePermissionWithBLOBs>  bsList= chargePermissionMapper.findPermissionAll(map);
            if (!bsList.isEmpty()){
                toJson.setObj(bsList);
                PageParams pageParams= (PageParams)map.get("pageParams");
                toJson.setTotleNum(pageParams.getTotal());
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    @Override
    public ToJson findPermissionById(ChargePermissionWithBLOBs chargePermission) {
        ToJson toJson=new ToJson(1,"暂无数据");
        try {
            ChargePermissionWithBLOBs chargePermissionWithBLOBs = chargePermissionMapper.selectByPrimaryKey(chargePermission.getChargePermissionId());
            if (chargePermissionWithBLOBs!=null){
                toJson.setObject(chargePermissionWithBLOBs);
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    @Override
    public ToJson enidOrInsertPermission(ChargePermissionWithBLOBs chargePermission) {
        ToJson toJson=new ToJson(1,"失败");
        try {
            int i=0;
            if (chargePermission.getChargePermissionId()!=null){
                i=chargePermissionMapper.updateByPrimaryKeySelective(chargePermission);
            }else{
                //遍历管理者
                //再循环里面，对每一个管理者进行判断是否存在，如果存在直接修改，否新增
                if (!StringUtils.checkNull(chargePermission.getManagerUsers())){
                    String[]  magerUser=chargePermission.getManagerUsers().split(",");
                    String[]  magerUserName=chargePermission.getManagerUsersName().split(",");
                    for (int j=0; i<magerUser.length;j++) {
                        String user=magerUser[j]; //userId
                        String userName=magerUserName[j]; //userName

                        chargePermission.setManagerUsers(user);
                        chargePermission.setManagerUsersName(userName);

                        Map<String, Object> map = new HashMap<>();
                        map.put("managerType",'3'); //类型为用户，管理者类型，1-部门，2-角色，3-用户',
                        map.put("managerUsers",user); //管理者
                        List<ChargePermissionWithBLOBs>  bsList= chargePermissionMapper.findPermissionAll(map);
                        if (bsList.size()>0){
                            ChargePermissionWithBLOBs chargePermissionWithBLOBs = bsList.get(0);
                            chargePermission.setChargePermissionId(chargePermissionWithBLOBs.getChargePermissionId());
                            chargePermissionMapper.updateByPrimaryKeySelective(chargePermission);
                        }else {
                            chargePermissionMapper.insertSelective(chargePermission);
                        }

                    }
                }
            }
            if (i>0){
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    @Override
    public ToJson delPermissionIds(ChargePermissionWithBLOBs chargePermission) {
        ToJson toJson=new ToJson(1,"失败");
        try {
            String[] split = chargePermission.getChargePermissionIds().split(",");
            int i = chargePermissionMapper.delPermissionIds(Arrays.asList(split));
            if (i>0){
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }
        }catch (Exception e){
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }
}
